package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ISystemDutyDao;
import com.tjsinfo.tjsoa.system.vo.TSystemDuty;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDuty;

/**
 * 系统用户职位Bo
 * @author Administrator
 *
 */
public class SystemUserDutyBo implements ISystemUserDutyBo{
	private IHibernateDao hibernateDao;
	private ISystemDutyDao systemDutyDao;
	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public ISystemDutyDao getSystemDutyDao() {
		return systemDutyDao;
	}

	public void setSystemDutyDao(ISystemDutyDao systemDutyDao) {
		this.systemDutyDao = systemDutyDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public Integer addSystemUserDuty(TSystemUserDuty systemUserDuty) {
		// TODO Auto-generated method stub

		return 		hibernateDao.save(systemUserDuty);
	}

	public Boolean deleteSystemUserDuty(Integer	 id) {
		// TODO Auto-generated method stub
		try {
			TSystemUserDuty systemUserDuty =  selectById(id);
			hibernateDao.delete(systemUserDuty);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public List<TSystemUserDuty> selectByUserId(Integer userid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserDuty.class);
		   criteria.add(Restrictions.eq("userId", userid));
		List<TSystemUserDuty> systemuserdutylist = qbcDao.getCriteria(criteria);
		List<TSystemUserDuty> list = new ArrayList<TSystemUserDuty>();
		for (TSystemUserDuty systemUserDuty : systemuserdutylist) {
			if(systemUserDuty !=null){
				TSystemDuty systemDuty = systemDutyDao.findById(systemUserDuty.getDutyId());
				if(systemDuty != null){
					String dutyname = systemDuty.getDutyName();
					systemUserDuty.setDutyName(dutyname);
					list.add(systemUserDuty);
				}
				
			}	
			
		}
		return list;
	}

	public TSystemUserDuty selectById(Integer id) {
		// TODO Auto-generated method stub
		return (TSystemUserDuty) hibernateDao.findById(TSystemUserDuty.class, id);
	}

	public Boolean deleteSysetemUserDutypByUserId(Integer userid) {
		// TODO Auto-generated method stub
		try {
			List<TSystemUserDuty> list = selectByUserId(userid);
			for (TSystemUserDuty systemUserDuty : list) {
				deleteSystemUserDuty(systemUserDuty.getId());
			}
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

}
